package com.dzt.androidframework.express;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.bigkoo.pickerview.OptionsPickerView;
import com.dzt.androidframework.R;
import com.dzt.androidframework.constant.AppConfig;
import com.dzt.androidframework.database.dao.TaskDao;
import com.dzt.androidframework.eventbus.EventBusUtil;
import com.dzt.androidframework.express.javabean.OrderDetailEvent;
import com.dzt.androidframework.express.javabean.OrderInfoBean;
import com.dzt.androidframework.express.javabean.OrderInfoLitePal;
import com.dzt.androidframework.express.net.MyStringCallBack;
import com.dzt.androidframework.express.net.Ok_Request;
import com.dzt.androidframework.fragment.FragmentBase;
import com.dzt.androidframework.utils.JLoadDlgKit;
import com.dzt.androidframework.utils.JSystemKit;
import com.dzt.androidframework.utils.JTimeKit;
import com.dzt.androidframework.utils.JToastKit;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by M02323 on 2017/9/18.
 */

@ContentView(R.layout.fragment_home)
public class FragmentHome extends FragmentBase {
	@ViewInject(R.id.et_num)
	EditText etNum;
	@ViewInject(R.id.et_company)
	EditText etCompany;

	@Override
	protected void initWidgets() {

	}

	@Event(value = {R.id.btn_query, R.id.iv_scan, R.id.iv_arrow})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_query:
				query();
				break;
			case R.id.iv_scan:
				break;
			case R.id.iv_arrow:
				JSystemKit.hideInputWindow(context, view);
				initOptionsPickerView();
				break;
			default:
				break;
		}
	}

	private void initOptionsPickerView() {
		final List<String> options1Items = CompanySingleton.getInstance().CompanyList;
		//条件选择器
		OptionsPickerView pvOptions = new OptionsPickerView.Builder(context,
				new OptionsPickerView.OnOptionsSelectListener() {
					@Override
					public void onOptionsSelect(int options1, int option2, int options3, View v) {
						//返回的分别是三个级别的选中位置
						String tx = options1Items.get(options1).toString();
						etCompany.setText(tx);
					}
				}).build();
		pvOptions.setPicker(options1Items);
		pvOptions.show();
	}

	public void query() {
		if (TextUtils.isEmpty(etNum.getText().toString()) || TextUtils.isEmpty(etCompany.getText().toString())) {
			JToastKit.getInstance().showMessage("单号或公司不能为空");
			return;
		}
		HashMap<String, String> map = new HashMap<>();
		map.put("type", CompanySingleton.getInstance().CompanyMap.get(etCompany.getText().toString()));//参数
		map.put("postid", etNum.getText().toString());
		//查询语句如：http://www.kuaidi100.com/query?type=shunfeng&postid=367659195777
		Ok_Request.getAsyncData(getActivity(), map, AppConfig.GetKuaiDi, new MyStringCallBack() {
			@Override
			public void onError(Call call, Exception e) {
				JToastKit.getInstance().showMessage(e.getMessage());
			}

			@Override
			public void onResponse(OrderInfoBean response) {
				if (response != null) {
					EventBusUtil.postSticky(new OrderDetailEvent(response, context));
					saveQuery(response);
					startActivity(OrderDetailActivity.class, null);
				}
			}

			@Override
			public void onBefore(Request request) {
				JLoadDlgKit.showProgressDialog(context, true);
			}

			@Override
			public void onAfter() {
				JLoadDlgKit.hideProgressDialog();
			}
		});
	}

	private void saveQuery(OrderInfoBean response) {
		TaskDao dao = TaskDao.getInstance();
		if (response.getMessage().equals("ok")) {
			List<OrderInfoLitePal> List = dao.findAllExpress();
			if (List == null || List.size() == 0) {
				OrderInfoLitePal bean = new OrderInfoLitePal();
				bean.setCom(response.getCom());
				bean.setNu(response.getNu());
				bean.setState(response.getState());
				bean.setTime(JTimeKit.getInstance().getNowTime());
				dao.saveExpress(bean);
			} else {
				for (OrderInfoLitePal bean : List) {
					if (bean.getNu().equals(response.getNu())) {
						//每查询一次，及时更新订单状态和查看时间
						dao.updateExpress(bean);
						break;
					}
				}
			}
		}
	}
}
